import { mapGetters } from 'vuex'
import { formatDate } from '@/filters/filter'
export default {
  computed: {
    ...mapGetters(['userInfo', 'ticket']),
    buriedpointpublic() {
      return {
        distinct_id: this.userInfo ? this.userInfo.username : '',
        user_id_type: 'P13',
        project_workspace: '数据资产治理办公室',
        project_subsidiary: '人寿保险公司',
        project_contact: 'chenyi-063',
        source_system: 'lF21MADS',
        behavior_subject: 'E',
        page_name: document.title,
        is_login: !!this.userInfo,
        date: formatDate(Date.now(), 'yyyy-MM-dd'),
        session_id: this.ticket,
        event_category: 'Common',
        // 驾驶舱下面分模块，使用module_name进行区分
        module_name: 'ceodashboard'
      }
    }
  },
  methods: {
    jsontoarr(json) {
      const names = []
      const jsonnamearr = Object.keys(json)
      jsonnamearr.forEach((e, i) => {
        names.push(e + '=')
        json[e].forEach(v => {
          Object.keys(v).forEach(d => {
            names.push(d + ':' + v[d] + ',')
          })
        })
        if (i !== jsonnamearr.length - 1) {
          names.push('|')
        }
      })
      return names.join('').split('|')
    },
    arrtojson(arr) {
      const json = {}
      arr.forEach(e => {
        const names = e.split('=')
        json[names[0]] = names[1]
      })
      return json
    },
    menuConfigFn(obj) {
      this.$sensors.track(obj.event, Object.assign(this.buriedpointpublic, obj, { time: Date.now() }))
    },
    // 预算考核，个险业务，个险队伍。公用事件
    clickMenuConfig(str) {
      const config = {
        预算考核: { event: 'budgetAssessment', event_name: '预算考核', page_name: '关键指标' },
        个险业务: { event: 'riskBusiness', event_name: '个险业务', page_name: '关键指标' },
        个险队伍: { event: 'riskTeam', event_name: '个险队伍', page_name: '关键指标' }
      }
      this.menuConfigFn(config[str])
    },
    // 下钻分析事件名称
    drillDownToAnalyse(data = {}, strjson = {}) {
      this.buryBommonFn('drillDownToAnalyse', '下钻分析', '下钻分析页', data, strjson)
    },
    // 交叉分析事件名称
    crossToAnalyse(data = {}, strjson = {}) {
      this.buryBommonFn('crossToAnalyse', '交叉分析', '交叉分析页', data, strjson)
    },
    // 归因分析事件名称
    attributionToAnalyse(data = {}, strjson = {}) {
      this.buryBommonFn('attributionToAnalyse', '归因分析', '归因分析页', data, strjson)
    },
    // 创建追踪
    createTraceForSensor(data = {}, strjson = {}) {
      this.buryBommonFn('createTrace', '创建追踪', '下钻或交叉详情页', data, strjson)
    },
    // 转发追踪
    transmitTraceForSensor(pageName, data = {}, strjson = {}) {
      this.buryBommonFn('transmitTrace', '转发追踪', pageName, data, strjson)
    },
    // 结束追踪
    endTraceForSensor(pageName, data = {}, strjson = {}) {
      this.buryBommonFn('endTrace', '结束追踪', pageName, data, strjson)
    },
    // 查询追踪列表
    traceListForSensor(data = {}, strjson = {}) {
      this.buryBommonFn('traceList', '查询追踪列表', '关于我', data, strjson)
    },
    // 查询@我追踪列表
    aboutMeTraceListForSensor(data = {}, strjson = {}) {
      this.buryBommonFn('aboutMeTraceList', '查询@我追踪列表', '关于我', data, strjson)
    },
    // 查看追踪详情
    traceDetailsForSensor(data = {}, strjson = {}) {
      this.buryBommonFn('traceDetails', '查看追踪详情', '追踪详情页', data, strjson)
    },
    /**
     * 绩效考核页面
     * */
    // 打开综合考核评分A类页面
    openPerformanceAPageForSensor(data = {}, strjson = {}) {
      this.buryBommonFn('performanceAPage', '打开综合考核评分A类页面', '综合考核评分A类页面', data, strjson)
    },
    // 打开综合考核评分B类页面
    openPerformanceBPageForSensor(data = {}, strjson = {}) {
      this.buryBommonFn('performanceBPage', '打开综合考核评分B类页面', '综合考核评分B类页面', data, strjson)
    },
    // 打开综合考核评分C类页面
    openPerformanceCPageForSensor(data = {}, strjson = {}) {
      this.buryBommonFn('performanceCPage', '打开综合考核评分C类页面', '综合考核评分C类页面', data, strjson)
    },
    buryBommonFn(event, eventName, pageName, data = {}, strjson = {}) {
      const obj = {
        event_name: eventName,
        time: Date.now(),
        event: event,
        page_name: pageName
      }
      const jsonParams = {}
      for (const name in data) {
        jsonParams[name] = []
        data[name].forEach(e => {
          jsonParams[name].push(JSON.stringify(e))
        })
      }
      this.$sensors.track(event, Object.assign(this.buriedpointpublic, obj, jsonParams, strjson))
    },
    // 登录 1可以替换ID  2是为了提供要抓取的数据
    sensorlogin(id) {
      this.$sensors.login(id)
      this.$sensors.track(
        'sensorLogin',
        Object.assign(this.buriedpointpublic, {
          event_name: '登录神测',
          event: 'sensorLogin',
          time: Date.now()
          // page_type: 'login_page'
        })
      )
    }
  }
}
